//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        primary: app.globalData.primary, // 主题色
        ossPath: app.globalData.ossPath,
        imgUrls: ['../../img/old/banner.png'], // banner图
        label: [],
        labelList: null,
        labels: [ // 链接标签
            {
                title: '纸质名片',
                src: '../../img/home/nav1.png',
                path: '../classification/classification?classnum=6&typeId=14'
            },
            {
                title: '每日一签',
                src: '../../img/home/nav2.png',
                path: '../classification/classification?classnum=8&typeId=62'
            },
            {
                title: '易拉宝',
                src: '../../img/home/nav3.png',
                path: '../classification/classification?classnum=6&typeId=28'
            },
            {
                title: '公众号封面',
                src: '../../img/home/nav4.png',
                path: '../classification/classification?classnum=8&typeId=48'
            },
            {
                title: '邀请函',
                src: '../../img/home/nav5.png',
                path: '../classification/classification?classnum=8&typeId=52'
            },
            {
                title: '淘宝广告',
                src: '../../img/home/nav6.png',
                path: '../classification/classification?classnum=9&typeId=66'
            },
            {
                title: '印刷海报',
                src: '../../img/home/nav7.png',
                path: '../classification/classification?classnum=6&typeId=26'
            },
            {
                title: '手机海报',
                src: '../../img/home/nav8.png',
                path: '../classification/classification?classnum=8&typeId=61'
            }

        ],
        animationData: {},
        floor: [], // 楼层信息
        scrollTop: 0 
    },
    onLoad: function () {
        //设置主题色
        if (app.globalData.primary) {
            this.setData({ primary: app.globalData.primary })
            app.usePrimary(app.globalData.primary)
        } else {
            // this.setData({ primary: app.globalData.primary })
            app.setPrimary = color => {
                this.setData({ primary: color })
            }
        }

        this.getLables();
        this.getFloor();
        
    },
    getFloor: function() { // 获取楼层信息
        wx.showLoading({title: '加载中'})
        wx.request({
            url: app.globalData.serverPath + '/RecommendTemplaleClass',
            success: ({data}) => {
                console.log(data)
                var floor = this.filterData(data)
                this.setData({ floor: floor})
            },
            complete: ()=> {
                wx.hideLoading()
            }
        })
    },
    filterData: function(data) { // 过滤
        var list = [];
        if (Array.isArray(data)) {
            data.forEach((product, j) => {
                list.push({
                    ID: product.ID,
                    ClassName: product.ClassName,
                    ClassNum: product.ClassNum,
                    ProductTypeList: [],
                    height: ''
                })
                let arr = []
                let newArr = []
                let TypeId = ''
                product.ProductTypeList.forEach((item, index) => {
                    index == 0 && (TypeId = item.TypeId)
                    let tmps = []
                    item.Templatelist.forEach((tmp, j) => {
                        tmps.push({
                            Name: tmp.Name,
                            TemplateNumber: tmp.TemplateNumber,
                            FacePicture: tmp.FacePicture,
                            Visits: tmp.Visits
                        })
                    })
                    
                    arr.push(...tmps)
                    
                })
                
                for (let i = 0; i < arr.length; i+=6) {
                    newArr.push(arr.slice(i, i+6))
                }

                list[j].ProductTypeList.push({
                    swiperIndex: 0,
                    TypeId: TypeId,
                    Templatelist: newArr
                })
            })
        }
        // console.log(list)
        return list
    },
    handleToggle: function(e) {
        var index = e.currentTarget.dataset.index;
        if (this.data.floor[index].ProductTypeList[0].swiperIndex < this.data.floor[index].ProductTypeList[0].Templatelist.length - 1) {
            this.data.floor[index].ProductTypeList[0].swiperIndex++;
        } else {
            this.data.floor[index].ProductTypeList[0].swiperIndex = 0
        }
        this.setData({
            floor: this.data.floor
        })
    },
    handleDesign: function(e) { // 去设计器
        app.checkedUserInfo().then(ok => {
            console.log(ok)
            if (!ok) return;
            var d = e.currentTarget.dataset.d;
            wx.setStorageSync('d', d)
            wx.setStorageSync('m', 0)
            var data = '?d=' + d + '&team=' + app.globalData.user.team + '&token=' + app.globalData.user.token
            wx.navigateTo({
                url: '../design/design' + data,
            })

        })
    },
    getHeight(index) { // 获取自适应高度
        var query = wx.createSelectorQuery();
        query.select('.swiperItem' + index).boundingClientRect()
        query.exec(res => {
            this.data.floor[index].height = res[0].height
            this.setData({
                floor: this.data.floor
            })
        })
    },
    handleLoad: function(e) { // 图片加载完
        var i = e.currentTarget.dataset.i;
        var imgIndex = e.currentTarget.dataset.index
        if (imgIndex <= 6) {
            // console.log(this.data.floor.ProductTypeList[index].height, index, i)
            this.getHeight(i)
        }
    },

    //获取首页便签推荐
    getLables: function() {
      var that = this
        wx.request({
          url: app.globalData.serverPath + '/mobile/products',
            method: 'get',
            success: ({data}) => {
              console.log(data)
              var arr = []
              data.forEach((item,index) => {
                item.ProductTypeList.forEach((tmp,i) => {
                  if (tmp.Recommend == true) {
                    arr.push(tmp)
                  }
                })
              })
              var label = that.filter(arr)
              console.log(label)
              that.setData({
                label: label
              })
            }
        })
    },
    filter: function(data) {
      var len = Math.ceil(data.length / 8), arr = [];
      for(var i=0;i<len;i++) {
        var item = data.splice(0,8)
        arr.push(item)
      }
      return arr
    },
    stopTouchMove: function() {},
    onShow: function () {
    },
    onReady: function () {
    },
    //监听屏幕滚动 判断上下滚动  
    onPageScroll: function (e) {
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function (e) {
    },
})